﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace Directory.Web
{
    public partial class Admin_ItemView : System.Web.UI.Page
    {
        protected int categoryId;
        protected int pageIndex;
        protected int pageSize = WebSettings.GetInstance().ItemViewPageSizeForAdmin;
        protected int totalRecords;
        protected string pageKey = "page";
        protected string categoryIdKey = "categoryId";
        protected string checkedKey = "ischecked";
        protected Directory.Model.CategoryInfo cat;

        protected void Page_Load(object sender, EventArgs e)
        {
            //RolePanel1.AllowRoles = RolePanel2.AllowRoles = WebSettings.GetInstance().RootCategoryAdminRole;
            if (!int.TryParse(Request.QueryString[categoryIdKey], out categoryId))
            {
                return;
            }
            if (!int.TryParse(Request.QueryString[pageKey], out pageIndex))
            {
                pageIndex = 0;
            }

            cat = CategoryDataProxy.GetCategory(categoryId);

            if (!IsPostBack)
            {
                bool ischecked;
                bool.TryParse(Request.QueryString[checkedKey], out ischecked);
                this.cbIsChecked.Checked = ischecked;
                if (ischecked)
                {
                    rptItems.DataSource = ItemDataProxy.GetItemsInCategory(categoryId, pageIndex, pageSize, out totalRecords);
                }
                else
                {
                    rptItems.DataSource = ItemDataProxy.GetUncheckedItemsInCategory(categoryId, pageIndex, pageSize, out totalRecords);
                }
                rptItems.DataBind();

                this.PageBreak.PageIndex = pageIndex;
                this.PageBreak.PageKey = pageKey;
                this.PageBreak.PageSize = pageSize;
                this.PageBreak.TotalRecords = totalRecords;
                this.PageBreak.NavigatorUrl = Request.Url.AbsolutePath + "?" + checkedKey + "=" + cbIsChecked.Checked.ToString() + "&" + categoryIdKey + "=" + categoryId.ToString() + "&";

                ltTitle.Text = string.Format("<span style='color:red;' title='{1}'>{0}</span>&nbsp; &nbsp; &nbsp;未审核{2} / 总计{2}：{3} / {4}", cat.CategoryName, "ID：" + cat.CategoryId.ToString() + "；", Resources.Resource.ResourceManager.GetString("Item"), ItemDataProxy.GetItemsCountInCategory(cat.CategoryId, false), cat.ItemsCount.ToString());
            }
        }
        protected void cbIsChecked_CheckedChanged(object sender, EventArgs e)
        {
            pageIndex = 0;
            if (this.cbIsChecked.Checked)
            {
                rptItems.DataSource = ItemDataProxy.GetItemsInCategory(categoryId, pageIndex, pageSize, out totalRecords);
            }
            else
            {
                rptItems.DataSource = ItemDataProxy.GetUncheckedItemsInCategory(categoryId, pageIndex, pageSize, out totalRecords);
            }
            rptItems.DataBind();

            this.PageBreak.PageIndex = pageIndex;
            this.PageBreak.PageKey = pageKey;
            this.PageBreak.PageSize = pageSize;
            this.PageBreak.TotalRecords = totalRecords;
            this.PageBreak.NavigatorUrl = Request.Url.AbsolutePath + "?" + categoryIdKey + "=" + categoryId.ToString() + "&" + checkedKey + "=" + cbIsChecked.Checked.ToString() + "&";
        }
    }
}